package GUI;
import controller.Controller;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class MerchantGUI {
    Controller controller = new Controller();
    public MerchantGUI()
    {
        edit();
    }
    private void edit() {
        JFrame frame = new JFrame("商家管理界面");
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        //
        //待加背景
        //

        //界面布局
        JPanel panel1 = new JPanel(new FlowLayout());
        JLabel label = new JLabel("本店的商品如下：");
        label.setFont(new Font("宋体",Font.BOLD,20));
        panel1.add(label);
        frame.add(panel1,BorderLayout.NORTH);

        //表格设计
        Vector<String> columnNames = createColumnNames();
        Vector<Vector<java.io.Serializable>> data = createData();//初始数据
        DefaultTableModel defaultModel = new DefaultTableModel(data,columnNames){
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };//表格不可编辑

        JTable table = new JTable(defaultModel);
        table.setFont(new Font("宋体",Font.PLAIN,20));
        table.getTableHeader().setFont(new Font("宋体",Font.PLAIN,20));
        table.setRowHeight(25);
        table.getTableHeader().setReorderingAllowed(false);
        JScrollPane scrollPane = new JScrollPane(
                table,
                ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER
        );
        //使表格内容居中
        DefaultTableCellRenderer dtcr = new DefaultTableCellRenderer();
        dtcr.setHorizontalAlignment(JLabel.CENTER);
        table.setDefaultRenderer(Object.class,dtcr);
        frame.add(scrollPane,BorderLayout.CENTER);
        //frame.setContentPane(scrollPane);
        //frame.add(panel2,BorderLayout.CENTER);



        //按钮设计及事件触发器
        JPanel buttonPanel = new JPanel(new GridLayout(3,3));


        JTextField text1 = new JTextField();
        JTextField text2 = new JTextField();
        JTextField text3 = new JTextField();
        JTextField text4 = new JTextField();
        JLabel label1 = new JLabel("商品名称");
        JLabel label4 = new JLabel(("商品标签"));
        JLabel label2 = new JLabel("单价（元）");
        JLabel label3 = new JLabel("数量");
        label4.setFont(new Font("宋体",Font.PLAIN,20));
        label1.setFont(new Font("宋体",Font.PLAIN,20));
        label2.setFont(new Font("宋体",Font.PLAIN,20));
        label3.setFont(new Font("宋体",Font.PLAIN,20));
        buttonPanel.add(label1);
        buttonPanel.add(label4);
        buttonPanel.add(label2);
        buttonPanel.add(label3);
        buttonPanel.add(text1);
        buttonPanel.add(text4);
        buttonPanel.add(text2);
        buttonPanel.add(text3);

        JButton button = new JButton(("刷新"));
        button.setFont(new Font("宋体",Font.PLAIN,20));
        button.setSize(200,10);
        JButton editButton = new JButton("编辑");
        editButton.setFont(new Font("宋体",Font.PLAIN,20));
        editButton.setSize(200,10);
        JButton addButton = new JButton("添加");
        addButton.setFont(new Font("宋体",Font.PLAIN,20));
        addButton.setSize(200,10);
        JButton deleteButton = new JButton("删除");
        deleteButton.setFont(new Font("宋体",Font.PLAIN,20));
        deleteButton.setSize(200,10);

        buttonPanel.add(button);
        buttonPanel.add(editButton);
        buttonPanel.add(addButton);
        buttonPanel.add(deleteButton);
        frame.add(buttonPanel,BorderLayout.SOUTH);

        //编辑商品的属性
        editButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                int selectedRow =table.getSelectedRow();
                if(selectedRow!=-1)
                {


                    defaultModel.setValueAt(text1.getText(), selectedRow, 1);//修改第二列
                    defaultModel.setValueAt(text2.getText(), selectedRow, 2);
                    defaultModel.setValueAt(text2.getText(), selectedRow, 3);
                    text1.setText(null);
                    text2.setText(null);
                    text3.setText(null);
                }
            }
        });
        //添加商品
        addButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub


                String id=null;//商品编号
                String[] rowData = {id,text1.getText(),text2.getText(),text3.getText()};//要增加的商品的属性
                defaultModel.addRow(rowData);
                int selectedRow =table.getSelectedRow()+1;
                text1.setText(null);
                text2.setText(null);
                text3.setText(null);

            }
        });


        //删除商品
        deleteButton.addActionListener(new  ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                int selectedRow =table.getSelectedRow();
                if(selectedRow!=-1)
                {
                    defaultModel.removeRow(selectedRow);
                }
            }
        });

        button.addActionListener(new ActionListener() {//刷新
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });

        frame.pack();
        frame.setVisible(true);
        frame.setBounds((Toolkit.getDefaultToolkit().getScreenSize().width)/2-300, (Toolkit.getDefaultToolkit().getScreenSize().height/2)-400, 600,600);

    }

    private Vector<String> createColumnNames()
    {
        Vector<String> vName = new Vector<String>();
        vName.add("商品编号");
        vName.add("商品名称");
        vName.add("商品标签");
        vName.add("单价(元）");
        vName.add("库存");
        return vName;
    }
    private Vector<Vector<java.io.Serializable>> createData()
    {
        //初始时的数据 数据要对象化
        Vector<Vector<java.io.Serializable>> data = new Vector<>();
        Vector<java.io.Serializable> rowData1 = new Vector<java.io.Serializable>();
        ArrayList<String> string = controller.merchantGoodsDataArrayList();
        //将要初始的数据添加到向量数组中
        for(int j = 0; j< string.size(); j++)
        {
            Vector<Serializable> Data = new Vector<>();
            String[] temp = string.get(j).split(" ");
            String[] data1 = {temp[0], temp[1], temp[2], temp[3],temp[4]};
            Data.add(temp[0]);
            Data.add(temp[1]);
            Data.add(temp[2]);
            Data.add(temp[3]);
            Data.add(temp[4]);
            data.add(Data);
        }

        return data;
    }
}
